import { Reducer, Effect } from 'umi';
import {
  getstudentList, //获取学生列表
  getcreateCluster, //按个人创建分群
  getClusterList, //获取分群列表
  getClusterDetail, //获取分群详情
  getClusterDelete, //删除分群
} from '@/services/createCluster';

//对state声明，state用于仓库存储数据，也就是说state就是仓库
export type StateType = {
  list?: any[];
  studentList?: any[];
};

//对model声明
export type createclusterModelType = {
  namespace: string;
  state: StateType;
  effects: {
    getstudentListEffects: Effect; //获取学生列表
    getcreateClusterEffects: Effect; //按个人创建分群
    getClusterListEffects: Effect; //获取分群列表
    getClusterDetailEffects: Effect; //获取分群详情
    getClusterDeleteEffects: Effect; //删除分群
  };
  reducers: {};
  subscriptions: {
    // setup: Subscription;
  };
};

const createclusterModel: createclusterModelType = {
  namespace: 'Model_createclusterModel',
  state: {
    list: [],
    studentList: [],
  },
  effects: {
    *getstudentListEffects({ payload }: any, { call, put }: any): Generator {
      //学生获取通知列表
      // let token = localStorage.getItem('token');
      const result: any = yield call(getstudentList, '', payload.code);
      const transformeData = result.result.map((item: any) => ({
        ...item,
        key: item.id,
      }));
      // console.log(transformeData)
      payload.callback(transformeData);
    },

    *getcreateClusterEffects({ payload }: any, { call, put }: any): Generator {
      //按个人创建分群
      // let token = localStorage.getItem('token');
      const result: any = yield call(getcreateCluster, '', payload.data);
      // console.log(result)
      payload.callback(result);
    },

    *getClusterListEffects({ payload }: any, { call, put }: any): Generator {
      //获取分群列表
      // let token = localStorage.getItem('token');
      const result: any = yield call(getClusterList, '', payload.code);
      // console.log(result)
      const transformeData = result.result.map((item: any) => ({
        ...item,
        key: item.id,
      }));
      payload.callback(transformeData);
    },

    *getClusterDetailEffects({ payload }: any, { call, put }: any): Generator {
      //获取分群详情
      // let token = localStorage.getItem('token');
      const result: any = yield call(getClusterDetail, '', payload.code);
      const transformeData = result.result.map((item: any) => ({
        ...item,
        key: item.id,
      }));
      // console.log(transformeData)
      payload.callback(transformeData);
    },

    *getClusterDeleteEffects({ payload }: any, { call, put }: any): Generator {
      //删除分群
      // let token = localStorage.getItem('token');
      const result: any = yield call(getClusterDelete, '', payload.code);
      // console.log(result)
      payload.callback(result);
    },
  },
  //命名规范（个人）：函数功能+Reducers
  reducers: {},
  subscriptions: {},
};

export default createclusterModel;
